﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;

namespace autobrowser
{
    public partial class Form1 : Form
    {
        private int urlno=0;
        private int urltotal;
        private XmlDocument xmlDoc = new XmlDocument();
        private   DataSet ds = new DataSet();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lburls.Items.Clear();
            cbgroup.Items.Clear();
            cbgroup.Items.Add("全部");

            ds.Clear();

            //加载xml文件，如不存在则新建一个
            try
            {
                ds.ReadXml("urls.xml");
            }
            catch
            {
                xmlcreate();
                ds.ReadXml("urls.xml");
            }

            //填充分类组合框
            urltotal = ds.Tables[0].Rows.Count;     //填充地址框列表
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)   //加载全部链接
            {
                cbgroup.Items.Add(ds.Tables[0].Rows[i]["UG"].ToString());
            }
            combomunique(cbgroup);

            urltotal = ds.Tables[0].Rows.Count;     //填充地址框列表
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)   //加载全部链接
            {
                lburls.Items.Add(ds.Tables[0].Rows[i]["Ua"].ToString());
            }

            cbgroup.Text = "全部";
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("urls.xml");

                XmlElement urls = doc.CreateElement("URLS");

                XmlElement url = doc.CreateElement("URL");

                XmlElement ug = doc.CreateElement("UG");
                ug.InnerText = tbug.Text;
                url.AppendChild(ug);

                XmlElement un = doc.CreateElement("UN");
                un.InnerText = tbun.Text;
                url.AppendChild(un);

                XmlElement ua = doc.CreateElement("UA");
                ua.InnerText = tbua.Text;
                url.AppendChild(ua);

                XmlElement root = doc.DocumentElement;
                root.AppendChild(url);
                doc.Save("urls.xml");
                MessageBox.Show("添加成功！", "提示");
            }
            catch (Exception xe)
            {
                MessageBox.Show(xe.Message);
            }
            Form1_Load(null, null);
        }

  

        private void btnstart_Click(object sender, EventArgs e)
        {
            urlno = 0;
            urltotal = lburls.Items.Count;
            lburls.SelectedIndex = 0;
            webBrowser1.Navigate(lburls.Items [0].ToString());
            tburltogo.Text = lburls.Items[0].ToString();
            timer1.Interval = Convert.ToInt16(tbtimerinteval.Text) * 1000;
            timer1.Enabled = true;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = Convert.ToInt16(tbtimerinteval.Text);
            timer1s.Enabled = true;
            cbgroup.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            urlno++;

            if (urlno == urltotal)
            {
                if (radioButton1.Checked == true)   //单次循环
                {
                    timer1.Enabled = false;
                    timer1s.Enabled = false;
                    MessageBox.Show("完成所有访问要求！","提示");
                }
                else                                //多次循环
                {
                    urlno = 0;
                    lburls.SelectedIndex = urlno;
                    webBrowser1.Navigate(ds.Tables[0].Rows[urlno]["ua"].ToString());
                    tburltogo.Text = ds.Tables[0].Rows[urlno]["ua"].ToString();
                }
            }
            else
            {
                lburls.SelectedIndex = urlno;
                webBrowser1.Navigate(ds.Tables[0].Rows[urlno]["ua"].ToString());
                tburltogo.Text = ds.Tables[0].Rows[urlno]["ua"].ToString();
            }
        }

        private void timer1s_Tick(object sender, EventArgs e)
        {
           
            if (progressBar1.Value == progressBar1.Maximum) progressBar1.Value = 0;
            progressBar1.PerformStep();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1s.Enabled =true  ;
            timer1.Enabled = true  ;
        }

        private void btngourl_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(tburltogo.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1s.Enabled = false;
            timer1.Enabled = false;
            cbgroup.Enabled = true;
            progressBar1.Value = 0;
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://zybsoft.com");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 aboutbox1 = new AboutBox1();
            aboutbox1.ShowDialog();
        }

        private void lburls_DoubleClick(object sender, EventArgs e)
        {
            tburltogo.Text = lburls.Items[lburls.SelectedIndex].ToString();
            webBrowser1.Navigate(tburltogo.Text);
        }

        private void lburls_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(lburls, lburls.Items[lburls.SelectedIndex].ToString());
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           
        }

        private void cbgroup_SelectedValueChanged(object sender, EventArgs e)
        {
            lburls.Items.Clear();
            ds.Clear();
            ds.ReadXml("urls.xml");
            urltotal = ds.Tables[0].Rows.Count;     //填充地址框列表
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)   //加载选定组
            {
                if (cbgroup .Text == "全部")
                { 
                    lburls.Items.Add(ds.Tables[0].Rows[i]["ua"].ToString()); 
                }
                else
                {
                    if (ds.Tables[0].Rows[i]["ug"].ToString() == cbgroup.Text)
                    {
                        lburls.Items.Add(ds.Tables[0].Rows[i]["ua"].ToString());
                    }
                }
            }

        }

        /// <summary>
        /// 创建缺省的xml文件
        /// </summary>
        /// <returns></returns>
        public bool xmlcreate()
        {
            XmlDocument doc = new XmlDocument();

            XmlDeclaration xmlDec = doc.CreateXmlDeclaration("1.0", "gb2312", null);

            XmlElement urls = doc.CreateElement("URLS");
            doc.AppendChild(urls);


            XmlElement url = doc.CreateElement("URL");

            XmlElement ug = doc.CreateElement("UG");
            ug.InnerText = "a";
            url.AppendChild(ug);

            XmlElement un = doc.CreateElement("UN");
            un.InnerText = "资源吧软件";
            url.AppendChild(un);

            XmlElement ua = doc.CreateElement("UA");
            ua.InnerText = "http://zybsoft.com";
            url.AppendChild(ua);

            XmlElement root = doc.DocumentElement;
            root.AppendChild(url);

            doc.InsertBefore(xmlDec, root);
            doc.Save("urls.xml");
            ds.ReadXml("urls.xml");

            return true;

        }

        private void btnurlmodify_Click(object sender, EventArgs e)
        {
            frm_modify urlmodify1 = new frm_modify();
            urlmodify1.ShowDialog();
            Form1_Load(null, null);
        }


        /// <summary>
        /// 清除组合框中的重复项
        /// </summary>
        /// <param name="cmb"></param>
        /// <returns></returns>
        private bool combomunique(ComboBox cmb)
        {
            int cmbno=cmb.Items .Count ;
            string  [] cmbtemp=new string[cmbno ];
         
            for (int i = 0; i < cmb.Items.Count; i++)
            {
                cmbtemp[i] = cmb.Items[i].ToString();
            }
            cmb.Items.Clear();
            
            bool find=false ;
            for (int i = 0; i < cmbno; i++)
            {
                find = false;
                for (int j = 0; j < i; j++)
                {
                    if (cmbtemp  [j] == cmbtemp [i])
                    {
                        find = true;
                        break;
                    }

                }
               if (find ==false)
               {
                   cmb .Items .Add (cmbtemp[i]);
               }
           }
           return true;
       }

        private void tburltogo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
                webBrowser1.Navigate(tburltogo.Text);
        }


    }
}
